<?php
/**
 * Created by PhpStorm.
 * User: zhixuan
 * Date: 2019/12/9
 * Time: 17:50
 */

namespace app\admin\controller;


use app\common\controller\BaseController;

use think\facade\Session;


class Base extends BaseController
{
    protected $return = ['code' => 300, 'msg' => '', 'data' => []];

    protected $admin_id;
    protected $admin_user;

    protected $module = 'admin';
    protected $controller = 'index';
    protected $action = 'index';

    protected function initialize()
    {
        parent::initialize();

        //验证登录
        $this->login_verify();
        //获取当前的模块名,控制器名,方法名
        $this->module = app('http')->getName();
        $this->controller = request()->controller();
        $this->action = request()->action();

        //验证权限
        $this->privilege_verify();
        //获取导航
        $this->getNav();
    }

    /*
     * 登录验证
     * */
    public function login_verify(){

        if(!Session::has('admin_id') && !Session::has('adminUser')){
            $this->redirect(url('admin/login/index')->suffix(false)->build());
        }

        $adminUser = Session::get('adminUser');
        if(!$adminUser){
            $this->failed('请登录',url('admin/login/index')->suffix(false)->build());
        }

        $this->admin_id = $adminUser->id;
        $this->admin_user = $adminUser;
        //获取用户最新信息
        $adminUser = \app\admin\model\Manage::getOne(['id'=>$this->admin_id]);
        if(!$adminUser){
            $this->failed('请登录',url('admin/login/index')->suffix(false)->build());
        }
        Session::set('adminUser',$adminUser);
        Session::set('admin_id',$adminUser->id);
        Session::save();

        $this->assign('admin',$this->admin_user);
    }

    //权限验证
    public function privilege_verify(){
        //过滤验证方法
        $action_arr = [
            'admin/Index/index',
            'admin/Index/systemIndex',
        ];
        $url = $this->module.'/'.$this->controller.'/'.$this->action;


        if(in_array($url,$action_arr)){
            return true;
        }

        //权限验证的条件
        $where = 'module_name = "'.$this->module .'" and controller_name = "'.$this->controller .'" and action_name = "'.$this->action.'"';
        //如果是超級管理員则拥有全部权限
        if($this->admin_id ==1){
            $count = \app\admin\model\Privilege::where($where)->count();
            if($count < 1){
                $this->failed('无权访问');
            }
        }else{
            $auth = \app\admin\model\Role::getRolePrivilege($this->admin_user->role_id);
            if(empty($auth)){
               $this->failed('无权访问');
            }

            $map = [];
            foreach($auth as $menu){
                if($menu->module_name == 'NULL') continue;
                $map[$menu->module_name.'/'.$menu->controller_name.'/'.$menu->action_name] = $menu->id;
            }

            if(!isset($map[$url])){
                $this->failed('无权访问');
            }
        }

    }

    //获取导航
    public function getNav(){
        $where = 'module_name = "' . $this->module . '" and controller_name = "' . $this->controller . '" and action_name = "' . $this->action . '"';

        $has = \app\admin\model\Privilege::getOne($where);
        $pri_id = !empty($has['id'])?$has['id']:'';
        $nav = \app\admin\model\Privilege::getFamily($pri_id);
        $this->assign('nav',$nav);
    }

}